/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Slave.java
 *
 * Created on 19/06/2012, 15:21:33
 */
package server.GUI;

import commom.VirtualMachine;
import javax.swing.AbstractListModel;
import commom.Network;

/**
 *
 * @author gravena
 */
public class Slave extends javax.swing.JFrame {

    private server.Slave slave;

    /** Creates new form Slave */
    public Slave() {
        initComponents();
        slave = null;
    }

    public Slave(server.Slave slave) {
        this.slave = slave;
        initComponents();
        this.listVM.setModel(new ListVMModel());
        System.out.println("Escravo: " + this.slave.getName());
    }

    private class ListVMModel extends AbstractListModel<VirtualMachine> {

        @Override
        public int getSize() {
            long checkSum_local = slave.getCheckSum();
            long checkSum_remote = Network.requestCheckSum(slave);

            if (checkSum_local != checkSum_remote) {
                // Recebe nova lista de máquinas virtuais
                System.err.println("Recebendo lista de máquinas virtuais...");
                Network.getVMList(slave);
            }
            return Slave.this.slave.getVMSize();
        }

        @Override
        public VirtualMachine getElementAt(int index) {
            long checkSum_local = slave.getCheckSum();
            long checkSum_remote = Network.requestCheckSum(slave);

            if (checkSum_local != checkSum_remote) {
                // Recebe nova lista de máquinas virtuais
                System.err.println("Recebendo lista de máquinas virtuais...");
                Network.getVMList(slave);
            }
            return Slave.this.slave.getVM(index);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelMain = new javax.swing.JPanel();
        labelNome = new javax.swing.JLabel();
        labelIP = new javax.swing.JLabel();
        labelAtivo = new javax.swing.JLabel();
        scrollListVM = new javax.swing.JScrollPane();
        listVM = new javax.swing.JList();
        buttonAddVM = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        panelMain.setBorder(javax.swing.BorderFactory.createTitledBorder("Slave"));

        labelNome.setText("Nome");

        labelIP.setText("IP");

        labelAtivo.setText("Ativo");

        listVM.setBorder(javax.swing.BorderFactory.createTitledBorder("Máquinas Virtuais"));
        listVM.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        scrollListVM.setViewportView(listVM);

        buttonAddVM.setText("Adicionar VM");

        javax.swing.GroupLayout panelMainLayout = new javax.swing.GroupLayout(panelMain);
        panelMain.setLayout(panelMainLayout);
        panelMainLayout.setHorizontalGroup(
            panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrollListVM, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
                    .addComponent(labelNome)
                    .addComponent(labelIP)
                    .addComponent(labelAtivo)
                    .addComponent(buttonAddVM, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        panelMainLayout.setVerticalGroup(
            panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMainLayout.createSequentialGroup()
                .addComponent(labelNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelIP)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelAtivo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollListVM, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonAddVM))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Slave().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAddVM;
    private javax.swing.JLabel labelAtivo;
    private javax.swing.JLabel labelIP;
    private javax.swing.JLabel labelNome;
    private javax.swing.JList listVM;
    private javax.swing.JPanel panelMain;
    private javax.swing.JScrollPane scrollListVM;
    // End of variables declaration//GEN-END:variables
}
